package com.umbrella.manage.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

	public static final String DATE_FORMAT = "yyyy-MM-dd";
	public static final String TIMESTAMP = "yyyy-MM-dd HH:mm:ss";
	public static final String TIMESTAMPSTR = "yyyyMMddHHmmss";
	/**
	 * 时间毫秒，转换为指定format的日期字符串
	 * 
	 * @param millis
	 * @param format
	 * @return
	 */
	public static String format(Long millis, String format) {
		if (StringUtils.isEmpty(format)) {

			format = DATE_FORMAT;
		}

		if (millis == null) {
			return null;
		}

		Date dateTime = new Date(millis);
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(dateTime);
	}

	/**
	 * 时间 转换为指定format的日期字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String format(Date date, String format) {
		if (StringUtils.isEmpty(format)) {

			format = DATE_FORMAT;
		}

		if (date == null) {
			return null;
		}

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	/**
	 * 指定format 的日期字符串，转换为Date
	 * 
	 * @param strDate
	 * @param format
	 * @return
	 */
	public static Date parseDate(String strDate, String format) {
		if (strDate == null || StringUtils.isEmpty(strDate))
			return null;

		try {
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			return formatter.parse(strDate);
		} catch (ParseException e) {
		}
		return null;
	}
	/**
	 * 
	 * @param format
	 * @return
	 */
	public static Long getCurrentTime(){
		SimpleDateFormat formatter = new SimpleDateFormat(TIMESTAMPSTR);
		return Long.valueOf(formatter.format(new Date()));
	}
	/**
	 * 
	 * @param format
	 * @return
	 */
	public static String getCurrentTime(String format){
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(new Date());
	}
	
	/**
	 * 获取当天0点时间： 20180820000000
	 * @return
	 */
	public static Long getTodayDate() {
		
		Calendar cal = Calendar.getInstance();

		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);

		SimpleDateFormat formatter = new SimpleDateFormat(TIMESTAMPSTR);
		return Long.valueOf(formatter.format(cal.getTime()));
	}
	
	public static Long getOffsetDateFromToday(Integer offsetDays) {
		
		Calendar cal = Calendar.getInstance();

		cal.add(Calendar.DAY_OF_YEAR, offsetDays);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);

		SimpleDateFormat formatter = new SimpleDateFormat(TIMESTAMPSTR);
		return Long.valueOf(formatter.format(cal.getTime()));
	}
	
	public static Long getOffsetMonthFromToday(Integer offsetMonth) {
		
		Calendar cal = Calendar.getInstance();

		cal.add(Calendar.MONTH, offsetMonth);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);

		SimpleDateFormat formatter = new SimpleDateFormat(TIMESTAMPSTR);
		return Long.valueOf(formatter.format(cal.getTime()));
	}
	
	public static void main(String[] args) {
		
		System.out.println(getTodayDate());
		System.out.println(getOffsetDateFromToday(1));
	}
}
